package com.baosight.sczx.common.zm.cInterface.cs;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.spring.SpringApplicationContext;
import com.baosight.iplat4j.dao.Dao;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.cmesI.IGenerateNumber;

/**
 * 
 * <p>Title: SystemSettingCSNumber.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 严洪浩
 * @version 1.0
 * <pre>Histroy:
 *          2011-5-30 上午11:26:55  严洪浩  Create
 *</pre>
 */
public class SystemSettingCSNumber implements IGenerateNumber {
	
	private static final String[] _WORKINGFACE = new String[]{"矿井","KJ"};
	private static final String[] _ROADWAY = new String[]{"矿井","KJ"};
	private static final String[] _OTHERDESIGN = new String[]{"矿井","KJ"};
	private static final String[] _ENGINEERING = new String[]{"矿井","KJ","巷道","HD","工作面","GZM"};
	private static Dao dao = (Dao)SpringApplicationContext.getBean("dao");
	
	private String params;
	
	public SystemSettingCSNumber(String _args){
		params = _args;
	}
	
	/**
	 * 验证
	 * <p>Function: checkValidator</p>
	 * <p>Description: </p>
	 * <p>return value: SystemSettingCSNumber</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-5-30上午11:26:40 严洪浩
	 * </p>
	 *
	 */
	public boolean checkValidator(String rule) {
		if(rule == null || params == null) {
			return false;
		}
		
		if(params.equals("1")) {
			//工作面
			return checkValidator(rule, _WORKINGFACE);
		}
		else if(params.equals("2")) {
			//巷道
			return checkValidator(rule, _ROADWAY);
		}
		else if(params.equals("3")) {
			//其他
			return checkValidator(rule, _OTHERDESIGN);
		}
		else {
			//工程
			return checkValidator(rule, _ENGINEERING);
		}
	}
	
	/**
	 * 判断是否合理
	 * <p>Function: checkValidator</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-5-30下午12:23:24 严洪浩
	 * </p>
	 *
	 */
	private boolean checkValidator(String rule, String[] _args) {
		String[] _RULE = rule.split("\\+");//根据+号截取规则
		boolean _result = false;
		
		for(int i = 0; i < _RULE.length; i++) {
			String _var = _RULE[i].toUpperCase();//大写
			
			if(i == (_RULE.length - 1)) {
				//最后一个, 判断是否是尾码
				if(_var.startsWith("尾码{")) {
					int _len = _var.length();
					if(_len != 7) {
						return false;
					}
					if(!_var.endsWith("}")) {
						return false;
					}
					String _int = _var.substring(5, 6);
					if(!StaticTools.checkInteger(_int)) {
						return false;
					}
				}
				else if(_var.startsWith("WM{")) {
					int _len = _var.length();
					if(_len != 5) {
						return false;
					}
					if(!_var.endsWith("}")) {
						return false;
					}
					String _int = _var.substring(3, 4);
					if(!StaticTools.checkInteger(_int)) {
						return false;
					}
				}
				else {
					return false;
				}
			}
			else {
				//判断是否在常规数据中
				for(String _OLD : _args) {
					if(_var.equals(_OLD)) {
						_result = true;
					}
					else if(_var.startsWith("[") && _var.endsWith("]")) {
						_result = true;
					}
				}
				
				//如果不在数组中
				if(!_result) {
					return false;
				}
			}
		}
		
		return true;
	}
	
	/**
	 * 
	 * <p>Function: GenerateNewNumber</p>
	 * <p>Description: </p>
	 * <p>return value: SystemSettingCSNumber</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-12上午01:57:57 严洪浩
	 * </p>
	 *
	 */
	public String generateNewNumber() {
		return null;
	}
	
	/**
	 * 生成新的编号
	 * 如果是工作面或者巷道 参数为工程设计ID
	 * 如果是工程 参数为 工作面、巷道、其他工程设计ID
	 * <p>Function: GenerateNewNumber</p>
	 * <p>Description: </p>
	 * <p>return value: SystemSettingCSNumber</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-5-30下午12:36:22 严洪浩
	 * </p>
	 *
	 */
	public String generateNewNumber(String _parentId) {
		if(_parentId == null || params == null) {
			return "";
		}
		
		StringBuffer number = new StringBuffer();
		String _RULE = "";
		
		if(params.equals("1")) {
			//工作面
			_RULE = SystemSettingCS.getInstance().getWorkingfaceNoRule();
		}
		else if(params.equals("2")) {
			//巷道
			_RULE = SystemSettingCS.getInstance().getRoadwayNoRule();
		}
		else {
			//其他
			_RULE = SystemSettingCS.getInstance().getOtherDesignNoRule();
		}
		
		String[] _RULES = _RULE.split("\\+");//根据+号截取规则
		for(int i = 0; i < _RULES.length; i++) {
			String _tmp = _RULES[i].toUpperCase();
			
			if(_tmp.equals("KJ") || _tmp.equals("矿井")) {
				String mineNo = EngineeringDesign.getFdMineNoByFdKeyId(_parentId);//_parentId是工程设计ID
				number.append(mineNo);
			}
			if(_tmp.startsWith("[") && _tmp.endsWith("]")) {
				number.append(_tmp.substring(1, _tmp.length() -1 ));
			}
			if(_tmp.startsWith("WM{") || _tmp.startsWith("尾码{")) {
				String _intTmp = _tmp.substring(_tmp.length()-2, _tmp.length()-1);
				int _int = Integer.parseInt(_intTmp);
				String oldNo = this.getMaxNumber(_parentId, null);//最大的一个编号parentId是工程设计ID
				String newNo = StaticTools.convertString(oldNo, oldNo.length() - _int, oldNo.length(),_int);
				number.append(newNo);
			}
		}
		
		return number.toString();
	}
	
	/**
	 * 生成新的编号
	 * 如果是工作面或者巷道 参数为工程设计ID
	 * 如果是工程 参数为 工作面、巷道、其他工程设计ID
	 * <p>Function: generateNewNumber</p>
	 * <p>Description: </p>
	 * <p>return value: SystemSettingCSNumber</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-13下午04:01:20 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String generateNewNumber(String[] _args) {
		if(_args == null || _args.length == 0 || params == null) {
			return "";
		}
		
		String fdKeyId = _args[0];
		String fdNo = _args[1];
		String tableName = _args[2];
		
		Map<String, String> _map = new HashMap<String, String>();
		_map.put("fdKeyId", fdKeyId);
		_map.put("tableName", tableName);
		
		StringBuffer number = new StringBuffer();
		if(params.equals("1")) {
			//工作面
		}
		else if(params.equals("2")) {
			//巷道
		}
		else if(params.equals("3")) {
			//其他
		}
		else {
			//工程 
			//fdKeyId为 工作面、巷道、其他设计的ID
			//fdTableName 为 表名
			String _parentId = "";//工程设计ID
			List _list = new ArrayList();
			_list = dao.query("ZMCSOtherDesign.queryDesign", _map);
			if(_list.size() <= 0) {
				return "";
			}
			else {
				Map<String, String> _result = (Map)_list.get(0);
				_parentId = _result.get("fdParentId");
			}
			
			String _RULE = SystemSettingCS.getInstance().getEngineeringNoRule();
			String[] _RULES = _RULE.split("\\+");//根据+号截取规则
			
			for(int i = 0; i < _RULES.length; i++) {
				String _tmp = _RULES[i].toUpperCase();
				
				if(_tmp.equals("KJ") || _tmp.equals("矿井")) {
					String mineNo = EngineeringDesign.getFdMineNoByFdKeyId(_parentId);
					number.append(mineNo);
				}
				if(_tmp.equals("HD") || _tmp.equals("巷道") || _tmp.equals("GZM") || _tmp.equals("工作面")) {
					number.append(fdNo);
				}
				if(_tmp.startsWith("[") && _tmp.endsWith("]")) {
					number.append(_tmp.substring(1, _tmp.length() -1 ));
				}
				if(_tmp.startsWith("WM{") || _tmp.startsWith("尾码{")) {
					String _intTmp = _tmp.substring(_tmp.length()-2, _tmp.length()-1);
					int _int = Integer.parseInt(_intTmp);
					String oldNo = this.getMaxNumber(fdKeyId, tableName);
					String newNo = StaticTools.convertString(oldNo, oldNo.length() - _int, oldNo.length(),_int);
					number.append(newNo);
				}
			}
		}
		
		return number.toString();
	}
	
	/**
	 * 根据规则获取最大的一个编号
	 * <p>Function: getWorkingfaceNo</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-5-31上午06:49:07 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private String getMaxNumber(String _args, String _tableName) {
		List<Map> _list = new ArrayList<Map>();
		Map<String, String> _map = new HashMap<String, String>();
		_map.put("orgCode", _args);
		
		try {
			if(params.equals("1")) {
				//工作面
				_list = dao.query("ZMCSWorkingFace.queryNextNumber", _map, 0, 1);
			}
			else if(params.equals("2")) {
				//巷道
				_list = dao.query("ZMCSRoadWay.queryNextNumber", _map, 0, 1);
			}
			else if(params.equals("3")) {
				//其他
				_list = dao.query("ZMCSOtherDesign.queryNextNumber", _map, 0, 1);
			}
			else {
				//工程
				_map.put("tableName", _tableName);
				_list = dao.query("ZMCSEngineering.queryNextNumber", _map, 0, 1);
			}
			
		} catch (Exception e) {
			
		}
		
		if(_list.size() > 0) {
			Map _result = _list.get(0);
			String oldNo = (String)_result.get("fdNo");
			return oldNo;
		}
		
		return "";
	}
}
